package top.ezzz.chats.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import top.ezzz.chats.entity.Users;
import top.ezzz.chats.service.IUserService;
import top.ezzz.common.R;
import top.ezzz.common.TimestampUtil;
import top.ezzz.common.UserBase;
import top.ezzz.config.AppException;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author shenmk
 * @since 2024-01-08
 */
@Api("全局接口")
@Controller
@Mapping("/chats")
public class LoginController {

    @Inject
    private IUserService userService;

    @ApiOperation("登录")
    @Get
    @Mapping("login")
    public R login(UserBase param) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>(new Users().toUsers(param));
        List<Users> list = userService.list(wrapper);
        if (list.size() == 0) {
            throw AppException.operation("用户信息不存在");
        }
        return R.ok(list.get(0));
    }

    @ApiOperation("注册")
    @Get
    @Mapping("register")
    public R register(Users param) {
        param.setCreateTime(TimestampUtil.getSeconds());
        boolean save = userService.save(param);
        return R.ok(save);
    }

    @ApiOperation("insert")
    @Post
    @Mapping("insert")
    public R insert(Users param) {
        boolean result =  userService.save(param);
        return R.ok(result);
    }

    @ApiOperation("update")
    @Put
    @Mapping("update")
    public R update(Users param) {
        boolean result =  userService.updateById(param);
        return R.ok(result);
    }

    @ApiOperation("delete")
    @Delete
    @Mapping("delete")
    public R delete(Users param) {
        boolean result =  userService.removeById(param);
        return R.ok(result);
    }


}
